package com.aplink.generic.xmlparser;

import org.w3c.dom.Node;

import android.text.TextUtils;

import com.aplink.generic.constant.ViewPagerContant;
import com.aplink.generic.pojo.controlspec.viewpager.ViewPagerSpec;

public class ViewPagerParser {
    public static ViewPagerSpec parse(final Node node) {
        final ViewPagerSpec pagerSpec = new ViewPagerSpec();
        pagerSpec.setPageCollection(PageCollectionParser.parse(node));
        if (node.hasAttributes()) {
            final String pageTransform = XMLPaserUtil.getAttrValue(node,
                    ViewPagerContant.PAGE_TRANSFORMER);
            if (!TextUtils.isEmpty(pageTransform)
                    && pageTransform.equalsIgnoreCase("Y")) {
                pagerSpec.setHasPageTransformer(true);
            }

            final String mViewpagerHeaderStyle = XMLPaserUtil.getAttrValue(
                    node, ViewPagerContant.HEADER_STYLE);
            if (!TextUtils.isEmpty(mViewpagerHeaderStyle)) {
                pagerSpec.setHeaderStyle(mViewpagerHeaderStyle);
            }
            final String offScreenLimit = XMLPaserUtil.getAttrValue(node,
                    ViewPagerContant.OFF_SCREEN_PAGE_LIMIT);
            if (!TextUtils.isEmpty(offScreenLimit)) {
                pagerSpec.setOffscreenPageLimit(Integer
                        .parseInt(offScreenLimit));
            }
            final String selectWhere = XMLPaserUtil.getAttrValue(node,
                    ViewPagerContant.SELECT_WHERE);
            if (!TextUtils.isEmpty(offScreenLimit)) {
                pagerSpec.setFilterBy(selectWhere);
            }
        }
        return pagerSpec;
    }
}
